<template>
<!--hotmall/packageB/addaddress/addaddress.wxml-->
<form action report-submit="true" @submit="submit">
   <view class="items">
      <text>姓名：</text>
      <!-- <input name="name" value='{{name}} 'bindinput='formName'/> -->
      <input name="name" :value="name" @input="formName"></input>
   </view>
   <view class="items">
      <text>手机号：</text>
      <input name="phone" placeholder="请输入手机号" :value="phone" @input="formPhone" type="number" maxlength="11"></input>
   </view>
  <view class="items">
    <text>省份：</text>
    <picker @change="bindPickerChange" :value="index" :range="array">
      <view class="picker">
      {{array[index]}}
      </view>
    </picker>
  </view>
  <view class="items">
    <text>城市：</text>
    <picker @change="bindPickerChangeOne" :value="indexone" :range="arrayone">
      <view class="picker">
        {{arrayone[indexone]}}
      </view>
    </picker>
  </view>
  <view class="items">
    <text>县区：</text>
    <picker @change="bindPickerChangeTwo" :value="indextwo" :range="arraytwo">
      <view class="picker">
        {{arraytwo[indextwo]}}
      </view>
    </picker>
  </view>
   <view class="items">
      <text>详细地址：</text>
      <input name="name" placeholder="请输入详细地址，如楼道、楼盘号等" :value="detail" @input="formDizhi"></input>
   </view>
   <radio-group @change="checkFun">
      <checkbox color="#8DE152" @change="checkFun" value="1" :checked="idefault"></checkbox>设置为默认地址
   </radio-group>
   <button @tap="submitone" formType="submit">保存</button>
</form>
</template>

<script>
// hotmall/packageB/editaddress/editaddress.js
import api from "../../../api";
var app = getApp();

export default {
  data() {
    return {
      res: [],
      county: '',
      name: '',
      phone: '',
      detail: '',
      province: '',
      city: '',
      area: '',
      id: '',
      provinceid: '',
      index: 0,
      indexone: 1,
      indextwo: 0,
      provinceidtone: '',
      array: '',
      arrayid: '',
      area_id: '',
      sheng: '',
      check: '',
      provinceidtwo: '',
      // 省份市区列表
      objectarray: [],
      isdefault: 0,
      idefault: false,
      uid: 0,
      objectarrayone: "",
      arrayone: "",
      arrayoneid: "",
      le: "",
      objectarraytwo: "",
      arraytwo: "",
      arraytwoid: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      res: options.res
    });
    console.log(this.res);
    this.getList();
    wx.getStorage({
      key: 'userId',
      success: res => {
        console.log(res.data);
        this.setData({
          uid: res.data
        }); // console.log(this.data.uid)
        // this.getList();
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    //获取收货地址列表
    getList() {
      app.globalData.request({
        url: api.default.detailsaddress,
        method: 'POST',
        data: {
          address_id: this.res
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);
          this.setData({
            id: res.address_id,
            name: res.name,
            phone: res.phone,
            detail: res.addr,
            area: res.area,
            sheng: res.province_id,
            city: res.city_id,
            area_id: res.area_id,
            index: res.province_id - 2,
            isdefault: res.is_default
          });

          if (this.isdefault == 1) {
            this.setData({
              idefault: true
            });
          } else if (this.isdefault == 0) {
            this.setData({
              idefault: false
            });
          }

          console.log(this.name); // 获取省

          this.getaddress();
        }
      });
    },

    //查询省
    getaddress() {
      app.globalData.request({
        url: api.default.get_region,
        //仅为示例，并非真实的接口地址
        data: {
          parent_id: 1
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        success: res => {
          // console.log(res)
          let name = [],
              index = [];

          for (let i = 0; i < res.length; i++) {
            name = name.concat(res[i].region_name);
            index = index.concat(res[i].region_id); // console.log(name)
          }

          this.setData({
            objectarray: res,
            array: name,
            arrayid: index,
            provinceid: this.sheng // objectarraytwo: null,
            // arraytwo: null,
            // arraytwoid: null

          });
          this.getaddressone(); // console.log(this.data.objectarray)
          // console.log(this.data.array)
          // console.log(this.data.arrayid)
        }
      });
    },

    //省
    bindPickerChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value);
      var id = this.arrayid[e.detail.value];
      console.log(id);
      this.setData({
        index: e.detail.value,
        provinceid: id
      });
      this.getaddressone();
    },
    // 获取城市索引值
    cont: function () {
      console.log(this.city);

      for (let a = 0; a < this.objectarrayone.length; a++) {
        console.log(this.objectarrayone[a].region_id);
        console.log(this.city); // console.log(this.data.city == this.data.objectarrayone[a].region_id)

        if (this.city == this.objectarrayone[a].region_id) {
          console.log(a);
          return a;
        }
      }
    },
    // 获取市区索引值
    contcity: function () {
      console.log(typeof this.area_id);

      for (let a = 0; a < this.objectarraytwo.length; a++) {
        // console.log(this.data.city == this.data.objectarrayone[a].region_id)
        if (this.area_id == this.objectarraytwo[a].region_id) {
          return a;
        }
      }
    },

    //获取市
    getaddressone() {
      app.globalData.request({
        url: api.default.get_region,
        data: {
          parent_id: this.provinceid
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        success: res => {
          console.log(res);
          let name = [],
              index = [];

          for (let i = 0; i < res.length; i++) {
            name = name.concat(res[i].region_name);
            index = index.concat(res[i].region_id); // console.log(name)
          }

          this.setData({
            objectarrayone: res,
            arrayone: name,
            arrayoneid: index
          }); // 市区索引

          console.log(this.cont());
          let city_index = this.cont();
          this.setData({
            indexone: city_index
          });
          this.getaddresstwo();
          console.log(this.indexone);
        }
      });
    },

    //市
    bindPickerChangeOne: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value);
      var id = this.arrayoneid[e.detail.value];
      console.log(id);
      this.setData({
        indexone: e.detail.value,
        city: id
      });
      this.getaddresstwo();
    },

    //获取区
    getaddresstwo() {
      app.globalData.request({
        url: api.default.get_region,
        //仅为示例，并非真实的接口地址
        data: {
          parent_id: this.city
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        success: res => {
          console.log(res);
          let name = [],
              index = [];

          for (let i = 0; i < res.length; i++) {
            name = name.concat(res[i].region_name);
            index = index.concat(res[i].region_id); // console.log(name)
          }

          this.setData({
            le: res.length,
            objectarraytwo: res,
            arraytwo: name,
            arraytwoid: index
          }); // 获取市区索引

          let qu_index = this.contcity();
          this.setData({
            indextwo: qu_index,
            county: name[0]
          }); // console.log(this.data.objectarray)
          // console.log(this.data.array)
          // console.log(this.data.arrayid)
        }
      });
    },

    //区
    bindPickerChangeTwo: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value);
      var id = this.arraytwoid[e.detail.value];
      console.log(id);
      this.setData({
        indextwo: e.detail.value,
        area_id: id
      });
    },

    //获取姓名
    formName(e) {
      // console.log(e)
      this.setData({
        name: e.detail.value
      });
      console.log(this.name);
    },

    //获取电话
    formPhone(e) {
      // console.log(e)
      this.setData({
        phone: e.detail.value
      });
      console.log(this.phone);
    },

    //获取详细地址
    formDizhi(e) {
      console.log(e);
      this.setData({
        detail: e.detail.value
      });
      console.log(this.detail);
    },

    checkFun: function (e) {
      console.log(e);

      if (e.detail.value == '') {
        this.setData({
          check: 0
        });
      } else {
        this.setData({
          check: 1
        });
      }
    },

    submitone() {
      this.submitone();
      var aa = /^[1]([3-9])[0-9]{9}$/;

      if (this.uid == 0) {
        wx.showToast({
          title: '用户id不能为空'
        });
      } else if (this.name == undefined || this.name == '') {
        wx.showToast({
          title: '请输入姓名'
        });
      } else if (this.phone == undefined || this.phone == '') {
        wx.showToast({
          title: '请输入手机号'
        });
      } else if (!aa.test(this.phone)) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        });
      } else if (this.provinceid < 2) {
        wx.showToast({
          title: '请选择省份'
        });
      } else if (this.provinceidone == 1) {
        wx.showToast({
          title: '请选择城市'
        });
      } else if (this.provinceidtwo == 0) {
        this.setData({
          provinceidtwo: this.county
        });
        console.log(this.county);
        this.submit();
      } else if (this.le == 0) {
        this.setData({
          le: 1,
          area_id: this.city
        });
        this.submit();
      } else if (this.detail == undefined) {
        wx.showToast({
          title: '请输入详细地址'
        });
      } else {
        console.log(this.id);
        console.log(this.uid);
        console.log(this.name);
        console.log(this.phone);
        console.log(this.provinceid);
        console.log(this.city);
        console.log(this.area_id);
        console.log(this.detail);
        console.log(this.check);
        app.globalData.request({
          url: api.default.editaddress,
          //仅为示例，并非真实的接口地址
          data: {
            address_id: this.id,
            uid: this.uid,
            name: this.name,
            phone: this.phone,
            province: this.provinceid,
            city: this.city,
            area: this.area_id,
            addr: this.detail,
            is_default: this.check
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值

          },
          success: res => {
            console.log(res);
            wx.showToast({
              title: '修改成功'
            });
            wx.navigateBack({
              detail: 1
            });
          }
        });
      }
    },

    submit(e) {
      console.log(e);
      var a = e.detail.formId;
      wx.getStorage({
        key: 'userId',
        success: function (aaa) {
          console.log(aaa.data);
          var open = aaa.data;
          console.log(open);

          if (a == 'the formId is a mock one') {} else {
            app.globalData.request({
              url: api.gzSea.test,
              //仅为示例，并非真实的接口地址
              data: {
                form_id: a,
                user_id: open
              },
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值

              },

              success(res) {
                console.log(res);
              }

            });
          }
        }
      });
    }

  }
};
</script>
<style>
@import "./editaddress.css";
</style>